package com.tosee.dao.allocine;

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class Test {
    private String URL_API_RECHERCHE = "http://api.allocine.fr/rest/v3/search";

    private String CODE_CHAINE = "q";

    private String CODE_PARTENAIRE = "partner";

    private String CODE_FILTER = "filter";

    private String CODE_FORMAT = "format";

    /**
     * @param args
     */
    public static void main(String[] args) {

        Test t = new Test();
        t.getInfo();
    }

    public void getInfo() {
        // Instantiate an HttpClient
        HttpClient client = new HttpClient();

        // Instantiate a GET HTTP method
        PostMethod method = new PostMethod(URL_API_RECHERCHE);
        method.setRequestHeader("Content-type", "text/xml; charset=ISO-8859-1");

        // Define name-value pairs to set into the QueryString
        NameValuePair nvp1 = new NameValuePair(CODE_CHAINE, "dexter");
        NameValuePair nvp2 = new NameValuePair(CODE_PARTENAIRE, "YW5kcm9pZC12M3M");
        NameValuePair nvp3 = new NameValuePair(CODE_FILTER, "tvseries");
        NameValuePair nvp4 = new NameValuePair(CODE_FORMAT, "XML");

        method.setQueryString(new NameValuePair[] { nvp1, nvp2, nvp3, nvp4 });

        try {
            int statusCode = client.executeMethod(method);

            System.out.println("Status Code = " + statusCode);
            System.out.println("QueryString>>> " + method.getQueryString());
            System.out.println("Status Text>>>" + HttpStatus.getStatusText(statusCode));

            // Get data as a String
            System.out.println(method.getResponseBodyAsString());

            JSONParser parser = new JSONParser();
            try {
                Object obj = parser.parse(method.getResponseBodyAsString());
                JSONObject jsonObject = (JSONObject) obj;

                // JSONObject json = (JSONObject) JSONSerializer.toJSON( jsonTxt );

                String name = (String) jsonObject.get("name");
                System.out.println(name);

                JSONArray array = (JSONArray) obj;
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            // OR as a byte array
            byte[] res = method.getResponseBody();

            // write to file
            FileOutputStream fos = new FileOutputStream("donepage.html");
            fos.write(res);

            // release connection
            method.releaseConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
